import {NButton, NFlex, NSelect, NTag} from 'naive-ui'
import {Router} from 'vue-router'
import {blog_del_api} from "@api/blog.ts";
import type {DialogApiInjection} from "naive-ui/es/dialog/src/DialogProvider";

const editBlog = async (id: number, router: Router) => {
    await router.push({name: 'blog-edit', query: {blog_id: id}})
}

async function delBlog(id: number, dialog: DialogApiInjection, reload: Function) {
    dialog.warning({
        title: '确定删除博客？',
        positiveText: '确认',
        negativeText: '取消',
        onPositiveClick: async () => {
            await blog_del_api(id)
            reload()
        }

    })
}

async function agreeComment(comment: any) {
    comment.state = 1
}

async function rejectComment(comment: any) {
    comment.state = 2
}

export const renderAction = (row: any, router: Router, dialog: DialogApiInjection, reload: Function) => {
    return <NFlex size={20}>
        <NButton type="primary" size="small" onClick={() => editBlog(row.id, router)}>编辑</NButton>
        <NButton type="error" size="small" onClick={() => delBlog(row.id, dialog, reload)}>删除</NButton>
    </NFlex>
}

export const renderCommentAction = (row: any) => {
    return <NFlex size={20}>
        <NButton type="primary" size="small" onClick={() => agreeComment(row)}>同意</NButton>
        <NButton type="primary" size="small" onClick={() => rejectComment(row)}>拒绝</NButton>
    </NFlex>
}
export const renderCommentState = (state: number) => {
    let type = ['primary', 'warning', 'error'][state]
    let info = ['待审核', '已同意', '已拒绝'][state]
    // @ts-ignore
    return <NTag type={type}> {info} </NTag>
}
const options = [
    {
        label: '待审核',
        value: 0
    },
    {
        label: '已同意',
        value: 1
    },
    {
        label: '已拒绝',
        value: 2
    }
]


export const renderCommentStateTitle = (callback: Function) => {
    // @ts-ignore
   return <NSelect options={options} placeholder='评论状态' onUpdate:value={callback}></NSelect>
}